package slxy.jiangtao.dts.controller.admin;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.entity.sys.Depart;
import slxy.jiangtao.dts.entity.sys.User;

import java.util.List;
import java.util.Optional;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/4/8.
 */
@Controller
@RequestMapping("depart")
public class DepartController {
    private static final Logger logger = LoggerFactory.getLogger(DepartController.class);
    @Autowired
    private CommonService commonService;

    @RequestMapping("index")
    public String addPage(ModelMap modelMap) {
        List<Depart> departList = commonService.getList(Depart.class);
        modelMap.put("departList", JSON.toJSONString(departList));
        return "systemmanager/depart";
    }

    @RequestMapping("add")
    @ResponseBody
    public JSONResult addSubmit(Integer parentlv, Integer pid) {
        Depart depart = new Depart();
        try {
            depart.setLv(parentlv + 1);
            depart.setName("new node");
            depart.setParentId(pid);
            commonService.save(depart);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return JSONResult.failure("添加失败");
        }
        return JSONResult.success(depart);
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Integer id) {
        try {
            commonService.deleteEntityById(Depart.class, id);
            //重置此部门相关摄影的部门
            Optional.ofNullable(commonService.findByProperty(User.class, "departId", id))
                    .ifPresent(userlist ->
                            userlist.forEach(user -> {
                                user.setDepartId(null);
                                commonService.updateEntity(user);
                            }));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return JSONResult.failure("删除失败");
        }
        return JSONResult.success("删除成功");
    }

    @RequestMapping("update")
    @ResponseBody
    public JSONResult update(Depart depart) {
        try {
            commonService.updateEntity(depart);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return JSONResult.failure("编辑失败");
        }
        return JSONResult.success("编辑成功");
    }

    @RequestMapping("info")
    public String info(Integer id,ModelMap modelMap){
        String sql ="SELECT count(*) FROM user WHERE depart_id =''"+id+" AND statu =0 ";
        Long count = commonService.getCountForJdbc(sql);
        return "systemmanager/departInfo";
    }
}
